import fitz
filename = "../demo.pdf"

search_term = "OVERCURRENT"

def save_png_by_search_keyword (filename, key_word):
    pdf_document = fitz.open(filename)
    for current_page in range(len(pdf_document)):
        page = pdf_document.loadPage(current_page)
        result_page = page.searchFor(key_word)
        if result_page:
            pix = page.get_pixmap(alpha=False)
            pix.writePNG("page-%i.png" % page.number)

# save_png_by_search_keyword(filename, search_term)


def save_png_set_pixmap__by_search_keyword (filename, key_word):
    # 解决清晰度问题 resolution 分辨率
    zoom_x = 3.0  # horizontal zoom
    zomm_y = 3.0  # vertical zoom
    mat = fitz.Matrix(zoom_x, zomm_y)  # zoom factor 2 in each dimension
    pdf_document = fitz.open(filename)
    for current_page in range(len(pdf_document)):
        page = pdf_document.loadPage(current_page)
        result_page = page.searchFor(key_word)
        if result_page:
            print("page.rect, ", page.rect)  # Rect(0.0, 0.0, 612.0, 792.0) 左上(0.0, 0.0) 右下(612.0, 792.0)
            pix = page.get_pixmap(matrix=mat)
            pix.writePNG("page-%i.png" % page.number)

# save_png_set_pixmap__by_search_keyword(filename, search_term)



# def save_png_set_pixmap_clip_by_search_keyword (filename, key_word):
#     # 解决清晰度问题 resolution 分辨率
#     zoom_x = 3.0  # horizontal zoom
#     zomm_y = 3.0  # vertical zoom
#     mat = fitz.Matrix(zoom_x, zomm_y)
#     left_top_gatter = (-10, -10)
#     left_top_x_position = 10
#     left_top_y_position = 10
#
#     pdf_document = fitz.open(filename)
#     for current_page in range(len(pdf_document)):
#         page = pdf_document.loadPage(current_page)
#         result_page = page.searchFor(key_word)
#         if result_page:
#             rect = page.rect
#             mp = (result_page[0][0] + left_top_gatter[0], result_page[0][1] + left_top_gatter[1])
#             print("mp: ", mp)
#             # print("rect.tl, rect.br: ", rect.tl, rect.br)  # Point(0.0, 0.0) Point(612.0, 792.0)
#             clip = fitz.Rect(mp, rect.br)       # 最终希望剪裁的区域
#             pix = page.get_pixmap(matrix=mat, clip=clip)
#             pix.writePNG("page-%i.png" % page.number)
#             # print("result_page:", result_page)
#             # print("page.rect, ", page.rect)  # Rect(0.0, 0.0, 612.0, 792.0) 左上(0.0, 0.0) 右下(612.0, 792.0)
#             # pix = page.get_pixmap(matrix=mat)
#             # pix.writePNG("page-%i.png" % page.number)

#
# def save_png_set_pixmap_clip_by_search_keyword(filename, key_word):
#     # 解决清晰度问题 resolution 分辨率
#     zoom_x = 3.0  # horizontal zoom
#     zomm_y = 3.0  # vertical zoom
#     mat = fitz.Matrix(zoom_x, zomm_y)
#     left_top_x_position = 20        # 左上x坐标
#     left_top_y_gatter_position = 0        # 左上y坐标向上留空白的gatter(读取关键词需要重新赋值)
#     right_bottom_x_gatter = 60
#     right_bottom_y_gatter = 60
#
#     pdf_document = fitz.open(filename)
#     for current_page in range(len(pdf_document)):
#         page = pdf_document.loadPage(current_page)
#         result_page = page.searchFor(key_word)
#         if result_page:
#             rect = page.rect
#             page_right_position = rect.br
#
#             # 剪裁的左上角的 (x, y) => x是相对固定值, 距离左边留有空余, y是关键词在文档中的位置向上移动gatter个像素
#             left_top_y_position = result_page[0][1] - left_top_y_gatter_position
#             right_bottom_x_position = page_right_position[0] - right_bottom_x_gatter
#             right_bottom_y_position = page_right_position[1] - right_bottom_y_gatter
#
#             clip_start = (left_top_x_position, left_top_y_position)
#             clip_end = (right_bottom_x_position, right_bottom_y_position)
#
#             # print("rect.tl, rect.br: ", rect.tl, rect.br)  # Point(0.0, 0.0) Point(612.0, 792.0)
#             clip = fitz.Rect(clip_start, clip_end)  # 最终希望剪裁的区域
#             pix = page.get_pixmap(matrix=mat, clip=clip)
#             pix.writePNG("page-%i.png" % page.number)
#
# save_png_set_pixmap_clip_by_search_keyword(filename, search_term)



def save_png_set_pixmap_clip_by_search_keyword(filename, key_word):
    # 解决清晰度问题 resolution 分辨率
    zoom_x = 3.0  # horizontal zoom
    zomm_y = 3.0  # vertical zoom
    mat = fitz.Matrix(zoom_x, zomm_y)
    left_top_x_position = 20        # 左上x坐标
    left_top_y_gatter_position = 0        # 左上y坐标向上留空白的gatter(读取关键词需要重新赋值)
    right_bottom_x_gatter = 60
    right_bottom_y_gatter = 60
    pdf_document = fitz.open(filename)
    for current_page in range(len(pdf_document)):
        page = pdf_document.loadPage(current_page)
        result_page = page.searchFor(key_word)
        if result_page:
            rect = page.rect
            page_right_position = rect.br

            # 剪裁的左上角的 (x, y) => x是相对固定值, 距离左边留有空余, y是关键词在文档中的位置向上移动gatter个像素
            left_top_y_position = result_page[0][1] - left_top_y_gatter_position
            right_bottom_x_position = page_right_position[0] - right_bottom_x_gatter
            right_bottom_y_position = page_right_position[1] - right_bottom_y_gatter

            clip_start = (left_top_x_position, left_top_y_position)
            clip_end = (right_bottom_x_position, right_bottom_y_position)

            # print("rect.tl, rect.br: ", rect.tl, rect.br)  # Point(0.0, 0.0) Point(612.0, 792.0)
            clip = fitz.Rect(clip_start, clip_end)  # 最终希望剪裁的区域
            pix = page.get_pixmap(matrix=mat, clip=clip)
            pix.writePNG("page-%i.png" % page.number)














